// gcc thread.c -o thread -lpthread

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *count();
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;

int main()
{
	int rc1, rc2;
	pthread_t thread1, thread2;

	// create thread, excute function count independent
	if(( rc1 = pthread_create(&thread1, NULL, &count, NULL) ))
	{
		printf("Thread creation failed: %d\n", rc1);
	}
	if(( rc2 = pthread_create(&thread2, NULL, &count, NULL) ))
	{
		printf("Thread creation failed: %d\n", rc2);
	}

	// wait for all thread complete
	pthread_join( thread1, NULL);
	pthread_join( thread2, NULL);
}

void *count()
{
	pthread_mutex_lock( &mutex );
	counter++;
	printf("Counter value: %d\n", counter );
	pthread_mutex_unlock( &mutex );
}